﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using OUSMS.AppLogic.BusinessLogicLayer;
using OUSMS.AppLogic.DataAccessLayer;
using OUSMS.Helpers;
namespace OUSMS
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (HttpContext.Current.User != null &&
                   HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    dateLbl.Text = DateTime.Now.ToString("yyyy 年 MM 月 dd 日");
                    int n = (int)DateTime.Now.DayOfWeek;
                    string[] weekDays = { "星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
                    weekLbl.Text = weekDays[n];

                    UsersBLL usersBLL = new UsersBLL();
                    Users userEntity = usersBLL.GetUserByLoginName(HttpContext.Current.User.Identity.Name);

                    userNameLbl.Text = userEntity.Name;

                    TreeNode node = new TreeNode();
                    node.Text = "科研管理系统";
                    userTreeView.Nodes.Add(node);                    

                    foreach (UserRoles userRole in userEntity.UserRoles)
                    {
                        TreeNode node1 = new TreeNode();
                        node1.Text = userRole.RoleType;
                        node1.SelectAction = TreeNodeSelectAction.Expand;
                        node.ChildNodes.Add(node1);

                        TreeNode node2 = new TreeNode();
                        switch (userRole.RoleType)
                        {
                            case "科研提案人":
                                node2.Text = "新建提案";
                                node2.NavigateUrl = "CreateMotion.aspx";
                                node1.ChildNodes.Add(node2);

                                node2 = new TreeNode();
                                node2.Text = "我的提案";
                                node2.NavigateUrl = "MyMotionList.aspx";
                                node1.ChildNodes.Add(node2);
                                break;
                            case "评审专家":
                                node2.Text = "待评审的提案";
                                node2.NavigateUrl = string.Format("WaitForFeedbackMotionList.aspx?Role=1");
                                node1.ChildNodes.Add(node2);

                                node2 = new TreeNode();
                                node2.Text = "已评审的提案";
                                node2.NavigateUrl = string.Format("MotionHistoryList.aspx?Role=1");
                                node1.ChildNodes.Add(node2);
                                break;
                            case "部门负责人":
                                node2.Text = "待评审的提案";
                                node2.NavigateUrl = string.Format("WaitForFeedbackMotionList.aspx?Role=2");
                                node1.ChildNodes.Add(node2);

                                node2 = new TreeNode();
                                node2.Text = "已评审的提案";
                                node2.NavigateUrl = string.Format("MotionHistoryList.aspx?Role=2");
                                node1.ChildNodes.Add(node2);
                                break;
                            case "校领导":
                                node2.Text = "待评审的提案";
                                node2.NavigateUrl = string.Format("WaitForFeedbackMotionList.aspx?Role=3");
                                node1.ChildNodes.Add(node2);

                                node2 = new TreeNode();
                                node2.Text = "已评审的提案";
                                node2.NavigateUrl = string.Format("MotionHistoryList.aspx?Role=3");
                                node1.ChildNodes.Add(node2);
                                break;
                            case "科研处管理人":
                                node2 = new TreeNode();
                                node2.Text = "用户管理";
                                node2.SelectAction = TreeNodeSelectAction.Expand;
                                node1.ChildNodes.Add(node2);

                                TreeNode node3 = new TreeNode();
                                node3.Text = "待审核用户";
                                node3.NavigateUrl = "ToCheckUserList.aspx";
                                node2.ChildNodes.Add(node3);

                                node3 = new TreeNode();
                                node3.Text = "用户管理";
                                node3.NavigateUrl = "UserManageList.aspx";
                                node2.ChildNodes.Add(node3);

                                node2 = new TreeNode();
                                node2.SelectAction = TreeNodeSelectAction.Expand;
                                node2.Text = "提案管理";
                                node1.ChildNodes.Add(node2);

                                node3 = new TreeNode();
                                node3.Text = "待预审提案";
                                node3.NavigateUrl = "WaitForPrejudicMotionList.aspx";
                                node2.ChildNodes.Add(node3);

                                node3 = new TreeNode();
                                node3.Text = "待转发提案";
                                node3.NavigateUrl = "WaitForTransferMotionList.aspx";
                                node2.ChildNodes.Add(node3);

                                node3 = new TreeNode();
                                node3.Text = "待立项提案";
                                node3.NavigateUrl = "WaitForEndMotionList.aspx";
                                node2.ChildNodes.Add(node3);

                                node3 = new TreeNode();
                                node3.Text = "已立项提案";
                                node3.NavigateUrl = "OKMotionList.aspx";
                                node2.ChildNodes.Add(node3);

                                node3 = new TreeNode();
                                node3.Text = "不立项提案";
                                node3.NavigateUrl = "NotOKMotionList.aspx";
                                node2.ChildNodes.Add(node3);

                                node2 = new TreeNode();
                                node2.SelectAction = TreeNodeSelectAction.Expand;
                                node2.Text = "系统设置";
                                node1.ChildNodes.Add(node2);

                                node3 = new TreeNode();
                                node3.Text = "部门维护";
                                node3.NavigateUrl = "DepartmentList.aspx";
                                node2.ChildNodes.Add(node3);

                                node3 = new TreeNode();
                                node3.Text = "研究领域维护";
                                node3.NavigateUrl = "StudyAreasList.aspx";
                                node2.ChildNodes.Add(node3);
                                break;
                        }
                    }
                }
            }
        }

        protected void exitLbl_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
        }
    }
}